<?php 
class web_truong
{
	function KetNoi($host,$user,$pass,$db)
	{
		$conn=mysql_connect($host,$user,$pass);
		if(!$conn)
		{
			echo "Lỗi kết nối ".mysql_error();
		}
		$select=mysql_select_db($db,$conn);
	}
	function QueryAll($sql)
	{
		$a=array();
		mysql_query("SET NAMES utf8");
		$query=mysql_query($sql);
		$i=0;
		while($row=mysql_fetch_assoc($query))
		{
			$a[$i]=$row;
			$i++;
		}
		return $a;
	}
	function QueryOne($sql)
	{
		mysql_query("SET NAMES utf8");
		$query=mysql_query($sql);
		$row=mysql_fetch_assoc($query);
		return $row;
	}
	function CallPage($name)
	{
		if(!isset($_REQUEST['option']))
		{
			$callstr=COM."trangchu".EXT;
		}
		else
		{
			$callstr=COM.$_REQUEST['option'].EXT;
		}
		return $callstr;
	}
	//Hàm phân trang
	function Pagination($total, $current, $limit, $url = '')
	{
        if($total==0) return '';
		$numPage = floor($total/ $limit);
        if (($total/ $limit) - $numPage > 0){
            $numPage += 1;
        }
        $html = '';
        if ($numPage == 1){
            return '';
        }
        if ($current == 1){
            $html .= "&nbsp;<a href='#'> Trang đầu </a>&nbsp;";
            $html .= "<a href='#'> Trước </a>&nbsp;";
        } else {
            $html .= "&nbsp;<a href='$url&page=1'> Trang đầu </a>&nbsp;";
            $html .= "<a href='$url&page=".($current - 1). "'> Trước </a>&nbsp;";
        }
        if($current <=3){
            for($i=1; ($i<=5) and ($i <= $numPage); $i++){
                if ($i == $current){
                    $html .= "<a href='#'> $i </a>&nbsp;";
                }else{
                    $html .= "<a href='$url&page=$i'> $i </a>&nbsp;";
                }
            }
        }else{
            if ($numPage >= $current + 2){
                for($i=$current-2; ($i <=$current+2) and ($i <= $numPage); $i++){
                    if ($i == $current){
                        $html .= "<a href='#'> $i </a>&nbsp;";
                    }else{
                        $html .= "<a href='$url&page=$i'> $i </a>&nbsp;";
                    }
                }
            }else{
                for($i = $numPage - 4;$i <= $numPage; $i++){
                    if($i > 0){
                        if ($i == $current){
                            $html .= $i. ' | ';
                        }else{
                            $html .= "<a href='$url&page=$i'> $i </a>&nbsp;";
                        }
                    }                        
                }
            }
        }
        
        if ($current == $numPage){
            $html .= "<a href='#'> Sau </a>&nbsp;";
            $html .= "<a href='#'> Trang cuối </a>&nbsp;";
        } else {
            $html .= "<a href='$url&page=".($current + 1). "'> Sau </a>&nbsp;";
            $html .= "<a href='$url&page=$numPage'> Trang cuối </a>&nbsp;";
        }
        return $html;
		
	}
	//Trang hien tại - Xử lý phân trang
    function PageCurrent()
	{
		if(!isset($_REQUEST['page']))
		{
			return 1;
		}
		else
		{
			return $_REQUEST['page'];
		}
	}
    //Đầu - Xử lý phân trang
    function FirstRecord($current,$limit)
    {
        if($current==1)
        {
            $dau=0;
        }
        else
        {
            $dau=($current-1)*$limit;
        }
        return $dau;
    }
    //Đầu - Xử lý phân trang
    function LastRecord($current,$limit,$total)
    {
        if($current==1)
        {
            $cuoi=($total<$limit)?$total:$limit;
        }
        else
        {
            $cuoi=$current*$limit;
            $cuoi=($cuoi>$total)?$total:$cuoi;
        }
        return ($cuoi-1);
    }
	function LinkToArray($link)
	{
		$sql="SELECT * FROM menu_left WHERE link='".$link."'";
		$row=$this->QueryOne($sql);
		return $row;
	}
	function IdToArray($id)
	{
		$sql="SELECT * FROM menu_left WHERE id='".$id."'";
		$row=$this->QueryOne($sql);
		return $row; 
	}

}
?>